<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="商品名称">
              <a-input placeholder="请输入商品名称" v-model="queryParam.name" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="商家名称">
              <a-input placeholder="请输入商家名称" v-model="queryParam.merchantName" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="一级类型">
              <a-select default-value="1" placeholder="全部类型" v-model="queryParam.catId" @change="catIdChange">
                <a-select-option :value="item.val" v-for="(item, index) in catIdUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="二级级类型">
              <a-select default-value="1" placeholder="全部类型" v-model="queryParam.catId2" >
                <a-select-option :value="item.val" v-for="(item, index) in catId2Ul" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="状态">
              <a-select default-value="1" placeholder="全部状态" v-model="queryParam.status" >
                <a-select-option :value="item.val" v-for="(item, index) in statusUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons" >
              <a-button type="primary" @click="searchQuery" icon="search" >查询</a-button >
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px" >重置</a-button >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id" :scroll="{ x: 1300 }" :columns="columns" :dataSource="tableData" :pagination="ipagination" :loading="loading" @change="handleTableChange" >
        <!--操作-->
        <span slot="action" slot-scope="text, record" >
          <a-button type="primary" @click="handleDetail(record)">详情</a-button>
          <!-- <a-button type="primary" @click="crudUpdate(record)">编辑</a-button> -->
          <!-- <a-popconfirm title="确认删除?" ok-text="确定" cancel-text="取消" @confirm="crudDelete(record.id)">
            <a-button type="danger" icon="delete"></a-button>
          </a-popconfirm> -->
        </span>
        <!--图片预览块-->
        <span slot="imgPath" slot-scope="text, record">
          <img :src="record.img" width="100px" height="100px" />
        </span>
        <!--上架/下架-->
        <!-- <span slot="switch" slot-scope="text, record">
          <a-switch
            :default-checked="record.isOnSale == 0 ? false : true"
            @change="onShowChange(record.isOnSale,record)"
            :disabled="isDisabledAuth('user:form:shelves')"
            checked-children="下架"
            un-checked-children="上架"
          >
            <a-icon slot="checkedChildren" type="check" />
            <a-icon slot="unCheckedChildren" type="close" />
          </a-switch>
          <br>
        </span> -->
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <goods-list-modal ref="updateForm" @closeModal="closeModal"></goods-list-modal>
  </a-card>
</template>

<script>
// import moment from 'moment'
import goodsListModal from './modules/goodsListModal'
import ApiTree from "@/api/goods/goodsList";
import Category from "@/api/common/category";
import { Slime, crud } from "@/mixins/slime";

export default {
  name: "goodsList",
  mixins: [Slime],
  components: {
    goodsListModal
  },
  data() {
    return {
      statusUl: [
        { name: "平台下架", val: 1 },
        { name: "待审核", val: 2 },
        { name: "审核通过", val: 3 },
        { name: "审核不通过", val: 4 },
        { name: "已上架", val: 6 },
      ],
      catIdUl:[
        // {val:0, name:''}
      ],
      catId2Ul:[
        // {val:0, name:''}
      ],
      tableData: [],
      columns: [
        {
          title: "商品id",
          align: "center",
          dataIndex: "id",
        },
        {
          title: "商品名称",
          align: "center",
          dataIndex: "name",
        },
        {
          title: "商家名称",
          align: "center",
          dataIndex: "merchantName",
        },
        {
          title: "一级类型",
          align: "center",
          dataIndex: "cat1Name",
        },
        {
          title: "二级类型",
          align: "center",
          dataIndex: "cat2Name",
        },
        {
          title: "商品图片",
          align: "center",
          dataIndex: "img",
          scopedSlots: { customRender: 'imgPath' },
        },
        {
          title: "原价",
          align: "center",
          dataIndex: "price",
          customRender: function(money) {
            return (parseInt(money) / 100).toFixed(2);
          }
        },
        {
          title: "现价",
          align: "center",
          dataIndex: "salePrice",
          customRender: function(money) {
            return (parseInt(money) / 100).toFixed(2);
          }
        },
        {
          title: "是否分店可用",
          align: "center",
          dataIndex: "subUse",
          customRender: function(subUse) {
            if(subUse == 0){
              return '否'
            }else{
              return '是'
            }
          }
        },
        {
          title: "店铺状态",
          align: "center",
          dataIndex: "status",
          customRender: function (status) {
            switch (status) {
              case 0:
                return "初始状态";
              case 1:
                return "平台下架";
              case 2:
                return "待审核";
              case 3:
                return "审核通过";
              case 4:
                return "审核不通过";
              case 5:
                return "已下架";
              case 6:
                return "已上架"; 
              default:
                return "未知状态";
            }
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
    };
  },
  created() {
    crud(ApiTree);
    this.loadData();
    this.catIdInit()
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
    catIdInit(){
      Category.getList({pid: 0}).then((res) => {
        this.catIdUl = res.result.map((obj)=>{
          return obj = {
            val: obj.id,
            name: obj.name, 
          }
        })
      })
    },
    //一级类目变化
    catIdChange(id){
      Category.getList({pid: id}).then((res) => {
        this.catId2Ul = res.result.map((obj)=>{
          return obj = {
            val: obj.id,
            name: obj.name, 
          }
        })
      })
    },
    handleDetail(record){
      this.$router.push({ path: '/goodsDetail?id='+record.id})
    },
    // 查 重置
    searchReset() {
      this.queryParam = {};
      this.queryParamRangePicker = null;
      this.catId2Ul = []
      this.$router.push({ query: {} })
      this.loadData(1);
    },
  },
};
</script>
<style scoped>
</style>